package com.Yukiha.gachamod.Data;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.util.*;

public class PlayerGachaData {

    private final Map<String, Set<String>> drawnModels = new HashMap<>();

    public boolean hasDrawn(String rarity, String model) {
        return drawnModels.getOrDefault(rarity, Set.of()).contains(model);
    }

    public void markDrawn(String rarity, String model) {
        drawnModels.computeIfAbsent(rarity, k -> new HashSet<>()).add(model);
    }

    public Set<String> getDrawnModels(String rarity) {
        return drawnModels.getOrDefault(rarity, Set.of());
    }

    // ✅ 新实现：返回所有模型 ID（不区分稀有度）
    public Set<String> getAllDrawnModels() {
        Set<String> all = new HashSet<>();
        for (Set<String> models : drawnModels.values()) {
            all.addAll(models);
        }
        return all;
    }

    // ✅ 新实现：移除任意模型（跨稀有度查找）
    public void removeModel(String modelId) {
        for (Set<String> models : drawnModels.values()) {
            models.remove(modelId);
        }
    }

    public JsonObject toJson() {
        JsonObject root = new JsonObject();
        for (var entry : drawnModels.entrySet()) {
            JsonArray array = new JsonArray();
            for (String model : entry.getValue()) {
                array.add(model);
            }
            root.add(entry.getKey(), array);
        }
        return root;
    }

    public static PlayerGachaData fromJson(JsonObject obj) {
        PlayerGachaData data = new PlayerGachaData();
        for (var entry : obj.entrySet()) {
            Set<String> set = new HashSet<>();
            for (JsonElement e : entry.getValue().getAsJsonArray()) {
                set.add(e.getAsString());
            }
            data.drawnModels.put(entry.getKey(), set);
        }
        return data;
    }
}
